<?php

namespace Client\Controller;

use Org\Net\Http;
use Think\Image;
use Think\Page;
use Think\Controller;

class AccountController extends Controller {
	/**
	 * 修改签名
	 */
	public function signature() {
		if (IS_POST) {
			// file_put_contents("Public/Log/log.txt", "post:".json_encode($_POST)."\r\n",FILE_APPEND|LOCK_EX);
			$account = D ( 'Account' );
			$data ['signature'] = $_POST ['signature'];
			$where ['id'] = $_POST ['account_id'];
			$result = $account->where ( $where )->save ( $data );
			if ($result) {
				$json ['result'] = 0;
				$json ['msg'] = '修改成功';
			} else {
				$json ['result'] = - 1;
				$json ['msg'] = '修改失败';
			}
			$this->ajaxReturn ( $json );
		}
	}
	/**
	 * 修改头像
	 */
	public function changeHeadimg() {
		if (IS_POST) {
			$file = $_FILES ['image'];
			if (sizeof ( $file ) > 0) {
				$path = "Public/Upload/HeadImage/";
				$util = A ( "Util" );
				$util->createDir ( $path );
				$path_info = pathinfo ( $file ['name'] );
				$file_extension = $path_info ["extension"];
				$file_name = uniqid ( "head_" ) . "." . $file_extension;
				$path_name = $path . $file_name;
				$image->open ( $file ['tmp_name'] );
				$image->save ( $path_name );
				$data ['headimgurl'] = 'http://' . $_SERVER ['HTTP_HOST'] . '/' . __ROOT__ . '/' . $path_name;
				$result = M ( "Account" )->where ( array (
						'id' => $_POST ['id'] 
				) )->save ( $data );
				if ($result) {
					$json ['result'] = 0;
					$json ['msg'] = '修改成功';
				} else {
					$json ['result'] = - 1;
					$json ['msg'] = '修改失败';
				}
				$this->ajaxReturn ( $json );
			}
		}
	}
	/**
	 * 获取用户信息
	 */
	public function getAccountById() {
		if (IS_POST) {
			// file_put_contents("Public/Log/log.txt", "post:".json_encode($_POST)."\r\n",FILE_APPEND|LOCK_EX);
			$account = D ( 'Account' );
			$where ['id'] = $_POST ['account_id'];
			$arr = $account->where ( $where )->field ( "id,nickname,headimgurl" )->find ();
			if ($arr) {
				$json ['result'] = 0;
				$json ['msg'] = '成功';
				$json ['account'] = $arr;
			} else {
				$json ['result'] = - 1;
				$json ['msg'] = '无此用户信息';
			}
			// file_put_contents("Public/Log/log.txt", "json:".json_encode($json)."\r\n",FILE_APPEND|LOCK_EX);
			$this->ajaxReturn ( $json );
		}
	}
	/**
	 * 第三方登录
	 */
	public function thirdLogin() {
		// file_put_contents("Public/Log/log.txt", "post:".json_encode($_POST)."\r\n",FILE_APPEND|LOCK_EX);
		if (IS_POST) {
			$account = D ( 'Account' );
			$types = array (
					'wx',
					'qq',
					'wb' 
			);
			$type = $_POST ['type'];
			if (in_array_case ( $type, $types )) {
				switch ($type) {
					case 'wx' :
						$unionid = $_POST ['unionid'];
						break;
					case 'qq' :
						$unionid = md5 ( $_POST ['headimgurl'] );
						break;
					case 'wb' :
						$unionid = $_POST ['unionid'];
						break;
				}
				
				$where ["{$type}_id"] = $unionid;
				$result = $account->where ( $where )->field ( "id,nickname,sex,headimgurl,birthday,signature,city,province" )->find ();
				// vendor ( "rongcloud.ServerAPI" );
				// $api = new \ServerAPI ();
				
				if ($result) {
					$json ['result'] = 0;
					$json ['msg'] = '登录成功';
					
					// $back = json_decode ( $api->getToken ( $result ['id'], $result ['nickname'], $result ['headimgurl'] ), 'json' );
					// $result ['token'] = $back ['token'];
					$json ['account'] = $result;
				} else {
					$util = A ( 'Util' );
					$data ['id'] = 'A' . $util->getId (); // 生成用户ID
					$data ["{$type}_id"] = $unionid;
					$data ['sex'] = $_POST ['sex'];
					$data ['nickname'] = $_POST ['nickname'];
					$data ['headimgurl'] = $_POST ['headimgurl'];
					$data ['city'] = $_POST ['city'];
					$data ['province'] = $_POST ['province'];
					$result = $account->add ( $data );
					$json ['result'] = 0;
					$json ['msg'] = '登录成功';
					
					// $back = json_decode ( $api->getToken ( $data ['id'], $data ['nickname'], $data ['headimgurl'] ), 'json' );
					// $data ['token'] = $back ['token'];
					
					$data ['birthday'] = "1990-01-01";
					$data ['status'] = "0";
					$data ['signature'] = '';
					$json ['account'] = $data;
				}
			} else {
				$json ['result'] = - 1;
				$json ['msg'] = '不支持改方式登录';
			}
			$this->ajaxReturn ( $json );
		}
	}
	/**
	 * 用户注册
	 */
	public function register() {
		if (IS_POST) {
			$account = D ( "Account" );
			$data = $account->create ( $_POST );
			if ($data) {
				$result = $account->add ( $data );
				if ($result) {
					$json ['result'] = 0;
					$json ['msg'] = "注册成功";
					
					vendor ( "rongcloud.ServerAPI" );
					$api = new \ServerAPI ();
					$back = json_decode ( $api->getToken ( $data ['id'], $data ['nickname'], $data ['headimgurl'] ), 'json' );
					
					$data ['token'] = $back ['token'];
					$data ['signature'] = '';
					$json ['account'] = $data;
				} else {
					$json ['result'] = - 1;
					$json ['msg'] = "注册失败";
				}
			} else {
				$json ['result'] = - 2;
				$json ['msg'] = $account->getError ();
			}
			// file_put_contents ( "Public/Log/log.txt", "json:" . json_encode ( $data ) . "\r\n", FILE_APPEND | LOCK_EX );
			$this->ajaxReturn ( $json );
		} else {
			$this->display ();
		}
	}
	/**
	 * 用户登录
	 */
	public function login() {
		if (IS_POST) {
			$where ['phone'] = $_POST ['phone'];
			$account = D ( 'Account' );
			$arr = $account->where ( $where )->field ( "id,nickname,sex,headimgurl,birthday,signature,city,province" )->find ();
			if ($arr) {
				if ($arr ['password'] == md5 ( $_POST ['password'] )) {
					$json ['result'] = 0;
					
					// vendor ( "rongcloud.ServerAPI" );
					// $api = new \ServerAPI ();
					// $back = json_decode ( $api->getToken ( $arr ['id'], $arr ['nickname'], $arr ['headimgurl'] ), 'json' );
					// $arr ['token'] = $back ['token'];
					
					$json ['account'] = $arr;
					$json ['msg'] = "登录成功";
				} else {
					$json ['result'] = - 1;
					$json ['msg'] = "密码错误";
				}
			} else {
				$json ['result'] = - 2;
				$json ['msg'] = "账号不存在";
			}
			$this->ajaxReturn ( $json );
		}
	}
	/**
	 * 重置密码
	 */
	public function resetPassword() {
		if (IS_POST) {
			$old = $_POST ['oldPwd'];
			$userid = $_POST ['id'];
			$new = $_POST ['newPwd'];
			$account = D ( 'Account' );
			if (md5 ( $old ) == $account->where ( array (
					'id' => $userid 
			) )->getField ( 'password' )) {
				$result = $account->where ( array (
						'id' => $userid 
				) )->setField ( 'password', md5 ( $new ) );
				if ($result) {
					$json ['msg'] = "密码已经成功修改！";
					$json ['result'] = 0;
					$this->ajaxReturn ( $json );
				} else {
					$json ['msg'] = "密码修改失败！";
					$json ['result'] = - 2;
					$this->ajaxReturn ( $json );
				}
			} else {
				$json ['msg'] = "旧密码错误，无法修改！";
				$json ['result'] = - 3;
				$this->ajaxReturn ( $json );
			}
		} else {
			$json ['msg'] = "数据不是以post方式提交";
			$json ['result'] = - 1;
			$this->ajaxReturn ( $json );
		}
	}
	/**
	 * 修改用户信息
	 */
	public function updateInfo() {
		if (IS_POST) {
			$data = $_POST;
			$result = M ( "Account" )->where ( array (
					'id' => $_POST ['id'] 
			) )->save ( $data );
			if ($result) {
				$json ['result'] = 0;
				$json ['msg'] = '修改成功';
			} else {
				$json ['result'] = - 1;
				$json ['msg'] = '修改失败';
			}
		}
	}
	/**
	 * 获取照片列表
	 */
	public function getImage() {
		$aid = I ( "aid" );
		if ($aid) {
			$list = M ( 'Publish' )->where ( array (
					'aid' => $aid 
			) )->field ( 'image' )->select ();
			$image = array ();
			foreach ( $list as $item ) {
				$image = array_merge ( $image, split ( ',', $item ['image'] ) );
			}
			if (sizeof ( $image ) > 0) {
				$json ['result'] = 0;
				$json ['msg'] = "获取照片列表成功";
				$json ['image'] = $image;
			} else {
				$json ['result'] = - 2;
				$json ['msg'] = "暂无照片，请去发布约吧";
			}
		} else {
			$json ['result'] = - 1;
			$json ['msg'] = "参数缺失";
		}
		$this->ajaxReturn ( $json );
	}
	/**
	 * 获取我的约吧列表
	 */
	public function getMyPublish() {
		$aid = I ( "aid" );
		if ($aid) {
			$page = I ( 'page' );
			$pageSize = 10;
			if ($page) {
				$start = $page * $pageSize;
			} else {
				S ( "MyPublish_{$aid}", NULL );
			}
			$list = S ( "MyPublish_{$aid}" );
			if ($list) {
				$list = D ( 'PublishView' )->where ( array (
						'aid' => $aid 
				) )->select ();
			}
			$res = array_slice ( $list, $start, $pageSize );
			if (sizeof ( $res ) > 0) {
				$json ['result'] = 0;
				$json ['msg'] = "获取我的约吧列表成功";
				$json ['publish'] = $res;
			} else {
				$json ['result'] = - 2;
				$json ['msg'] = "暂无约吧信息，快去去发布吧";
			}
		} else {
			$json ['result'] = - 1;
			$json ['msg'] = "参数缺失";
		}
		$this->ajaxReturn ( $json );
	}
	/**
	 * 删除我的约吧信息
	 */
	public function deleteMyPublish() {
		$aid = I ( 'aid' );
		$pid = I ( 'pid' );
		if ($aid && $pid) {
			$where ['id'] = $pid;
			$where ['aid'] = $aid;
			$result = M ( 'Publish' )->where ( $where )->delete ();
			if ($result) {
				$json ['result'] = 0;
				$json ['msg'] = "删除我的约吧成功";
			} else {
				$json ['result'] = - 2;
				$json ['msg'] = "删除我的约吧失败";
			}
		} else {
			$json ['result'] = - 1;
			$json ['msg'] = "参数缺失";
		}
		$this->ajaxReturn ( $json );
	}
}